/**
 * Created With IntelliJ IDEA
 * Description:牛客网：JZ31 栈的压入、弹出序列
 * <a href="https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&&tqId=11174&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking">...</a>
 * User: DELL
 * Data: 2023-01-04
 * Time: 23:41
 */


//题目规定
//1. 0<=pushV.length == popV.length <=1000
//2. -1000<=pushV[i]<=1000
//3. pushV 的所有数字均不相同
import java.util.*;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        //判空处理
        if (pushA.length == 0) {
            return true;
        }
        //辅助栈
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int k : pushA) {
            //压入数据
            stack.push(k);
            //比对弹出数据
            //1.不需要判断j<popA.length,因为题目中规定pushV.length == popV.length
            //2.这里不可以写成stack.peek() == popA[j],因为stack.peek()弹出的是Integer类型的数据
            while (stack.size() > 0 && stack.peek().equals(popA[j])) {
                stack.pop();
                j++;
            }
        }
        return stack.size() == 0;
    }
}